%% Temple university CIS 2033
% A modern Introduction to Probability and Statistics
% Chapter 10: Covariance and correlation
%
% Author: Djordje Gligorijevic
% email: gligorijevic@temple.edu
%
% Special thanks to Min Xiao (http://astro.temple.edu/~tud09663/index.html)
% for given examples and ideas for lab examples.

%% Excercise 1: load the demo5.mat
% 1.1) compute the covariance between each column of X and Y;
% 1.2) compute the correlation coefficient between each column of X and Y;

load demo5.mat;

covariances = zeros(1, size(X,2));
for i = 1:size(X,2)
    C = cov(X(:,i), Y)
    covariances(i) = C(1,2);
end

correlation_coefficient = zeros(1, size(X,2));
for i = 1:size(X,2)
    C = corrcoef(X(:,1), Y);
    correlation_coefficient(i) = C(1,2);
end

%% Excercise 2: load station precipitation data
% 2.1) Calculate covariance between first 5 stations in the dataset
% 2.2) Plot correlation matrix of first 5 stations in the dataset using the
% corrplot function in MATLAB

load precipitation_north_east.mat;
y(isnan(y)) = 0;

covariances = zeros(5, 5);
for i = 1:5
    for j = 1:5
        C = cov(y(i,:), y(j,:));
        covariances(i,j) = C(1,2);
    end
end

corrplot(y(1:5,:)')
